package com.sky.aspect;

import com.sky.anno.autoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.entity.Employee;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {


    //抽取切入点
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.anno.autoFill)")
    public void autoFillPointcut(){}

    @Before("autoFillPointcut()")
    public void autoFillAspect(JoinPoint joinPoint){
        log.info("公共字段自动填充启动了");
        //获取被拦截的注解方法上的数据类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();//方法签名对象
        autoFill autoFill = signature.getMethod().getAnnotation(autoFill.class);//获取方法上面的注解
        OperationType operationType = autoFill.value();//获取注解里面属性值
        //获取当前被拦截的参数 实体对象
        Object[] args = joinPoint.getArgs();
        if (args ==null || args.length==0){
            return;
        }
        Object entry=args[0];
        //准备要赋值的参数
        LocalDateTime now = LocalDateTime.now();
        Long currentId = BaseContext.getCurrentId();
        if (operationType==OperationType.INSERT){
            //通过反射给方法进行赋值
            try {
                //通过反射获取实体类方法
                Method setcreateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setupdateTime = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setcreateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setupdateUser = entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                //用反射进行赋值
                setcreateTime.invoke(entry,now);
                setupdateTime.invoke(entry,now);
                setcreateUser.invoke(entry,currentId);
                setupdateUser.invoke(entry,currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }else if (operationType==OperationType.UPDATE){
            try {
                entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME,LocalDateTime.class).invoke(entry,now);
                entry.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER,Long.class).invoke(entry,currentId);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        }

    }
}
